package com.itlaobing.web.management.controller;

import com.itlaobing.pojo.Dept;
import com.itlaobing.service.DeptService;
import com.itlaobing.util.PageUtil;
import com.itlaobing.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(path = "/management/dept")
public class ManagementDeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 后台所有用户显示,分页
     * @return
     */
    @RequestMapping(path = "/page/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String userList(Integer pageIndex, String condition, Model model){

        //创建分页实体类对象
        PageUtil<Dept> page = new PageUtil<Dept>();

        //判断条件
        if(StringHelper.empty(condition)){
            condition="";
        }
        page.setCondition(condition);

        //获取总条数
        int count = deptService.findCountByPage(page);
        page.setRecordCount(count);

        //判断当前页
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageIndex<=1){
            pageIndex=1;
        }
        if(pageIndex>=page.getPageCount()){
            pageIndex=page.getPageCount();
        }
        page.setPageIndex(pageIndex);

        //设置页码
        page.setPageNums(5);

        //获取分页数据
        List<Dept> depts = deptService.findAllByPage(page);
        page.setDatas(depts);

        model.addAttribute("putil",page);

        return "/management/user/list";
    }
}
